package day17;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.Scanner;

public class HomeWork{}

class A3 implements Runnable {
    private int n;

    public A3(int n) {
        this.n = n;
    }

    public void run() {
        for (int i = 0; i < n; i++) {
            LocalTime ld = LocalTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
            String formattedTime = ld.format(formatter);
            System.out.println("时间: " + formattedTime);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入循环次数:");
        int n = sc.nextInt();
        Runnable a = new A3(n);
        Thread thread = new Thread(a);
        thread.start();
    }
}

class B3{
    public static void main(String[] args) {
        Random r = new Random();
        Runnable a = () -> {
            System.out.println("温度:"+(r.nextInt(21)+20));
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
        Runnable b = () -> {
            System.out.println("湿度:"+(r.nextInt(21)+70));
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
        Runnable c = () -> {
            System.out.println("光线强度:"+(r.nextInt(61)+20));
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
        Thread threadA = new Thread(a);
        threadA.start();
        Thread threadB = new Thread(b);
        threadB.start();
        Thread threadC = new Thread(c);
        threadC.start();
    }
}

class A4{
    public synchronized void Access() throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name + "开始进洞");
        System.out.println("剩余3s");
        Thread.sleep(1000);
        System.out.println("剩余2s");
        Thread.sleep(1000);
        System.out.println("剩余1s");
        Thread.sleep(1000);
        System.out.println(name + "开始出洞");
    }
    public static void main(String[] args) {
        String[] name = new String[10];
        Random r = new Random();
        A4 a = new A4();
        for (int i = 0; i < 10; i++) {
            name[i] = (char)(r.nextInt(26)+97)+""+(char)(r.nextInt(26)+97);
            new Thread(() -> {
                try {
                    a.Access();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },name[i]).start();
        }
    }
}

class B4{
    public static void main(String[] args) {
        Thread T1 = new Thread(() -> {
            System.out.println("T1正在运行");
        });
        Thread T2 = new Thread(() -> {
            try {
                T1.join();
                System.out.println("T2正在运行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread T3 = new Thread(() -> {
            try {
                T2.join();
                System.out.println("T3正在运行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        T1.start();
        T2.start();
        T3.start();
    }
}